package vip.xiaonuo.biz.modular.scan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.apilog.entity.DevApiLog;
import vip.xiaonuo.biz.modular.cabinetdoor.vo.BizCabinetDoorResp;
import vip.xiaonuo.biz.modular.cabinetinfo.mapper.BizCabinetInfoMapper;
import vip.xiaonuo.biz.modular.event.entity.BizEvent;
import vip.xiaonuo.biz.modular.event.entity.BizEventConfig;
import vip.xiaonuo.biz.modular.event.mapper.BizEventConfigMapper;
import vip.xiaonuo.biz.modular.event.mapper.BizEventMapper;
import vip.xiaonuo.biz.modular.event.service.BizEventReplaceService;
import vip.xiaonuo.biz.modular.event.util.EventActionUtil;
import vip.xiaonuo.biz.modular.goods.entity.BizGoods;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsMapper;
import vip.xiaonuo.biz.modular.goods.result.GoodsLedgerPageResult;
import vip.xiaonuo.biz.modular.goods.service.BizGoodsService;
import vip.xiaonuo.biz.modular.scan.param.ScanDto;
import vip.xiaonuo.biz.modular.scan.param.ScanItemDto;
import vip.xiaonuo.biz.modular.scan.service.ScanEventService;
import vip.xiaonuo.common.util.AliyunSmsUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 墨塵
 * @since 2023/11/1
 */
@Service
@Transactional
public class ScanEventServiceImpl implements ScanEventService {

    @Resource
    private BizEventConfigMapper bizEventConfigMapper;

    @Resource
    private BizEventReplaceService bizEventReplaceService;

    @Resource
    private BizEventMapper bizEventMapper;

    @Resource
    private BizGoodsService bizGoodsService;

    @Resource
    private BizGoodsMapper bizGoodsMapper;

    @Resource
    private BizCabinetInfoMapper bizCabinetInfoMapper;


    @Override
    public void addRecordForGoodsFirstInCab(List<BizGoods> goodsList, DevApiLog apiLog) {
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("16");
        //柜门名称
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        for (BizGoods goods : goodsList) {
            String eventDesc = eventConfig.getEventDesc();
            //事件描述
            String goodsName = bizEventReplaceService.getGoodsNameForEntity(goods);
            eventDesc = eventDesc.replace("{{goods}}", goodsName);
            eventDesc = eventDesc.replace("{{cab-door}}", doorName);
            eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
            eventDesc = eventDesc.replace("{{action}}", EventActionUtil.getApiAction(apiLog.getReqType()));
            String userName = bizEventReplaceService.getUserName(apiLog.getUserId());
            eventDesc = eventDesc.replace("{{user}}", userName);
            //组合实体类
            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
            event.setApiLogId(apiLog.getId());
            event.setEventConfigId(eventConfig.getId());
            event.setEventTime(apiLog.getEventTime());
            event.setEventDesc(eventDesc);
            event.setEventUserId(apiLog.getUserId());
            event.setGoodsId(goods.getId());
            event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
            event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
            event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
            event.setCabinetDoorId(cabinetDoorResp.getId());
            bizEventReplaceService.clearBizEvent(event);
            //类目切换
            bizEventReplaceService.itemSwitch(event, goods.getGoodsType());
            bizEventMapper.insert(event);
            //记录台账-首次入柜
            bizGoodsService.handleLedgerForEventOfGoodsInCab(goods, apiLog);

            //【邮件】本次刷卡开门的卡主，柜门管理员
            bizEventReplaceService.sendEmailForUser(apiLog.getUserId(), event);
            bizEventReplaceService.sendEmailForDoorAdmin(event);
        }
    }

    @Override
    public void handleReagentInCabFirst(ScanDto dto, DevApiLog apiLog) {
        if (dto.getGoodsType() == 1) {
            //list-首次入库未称重
            List<BizGoods> noWeighList = new ArrayList<>();
            //柜门名称
            String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
            BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
            //查询对应配置
            BizEventConfig eventConfig = bizEventConfigMapper.selectById("41");
            for (ScanItemDto scanItem : dto.getItemList()) {
                BizGoods goods = bizGoodsMapper.selectById(scanItem.getGoodsId());
                if (null == goods) {
                    continue;
                } else {
                    if (goods.getGoodsType() == 1 && goods.getEnterFlag() == 0) {
                        if (StringUtils.isNotBlank(scanItem.getWeigh())) {
                            //更新第一次称重数据
                            bizGoodsMapper.updateFirstWeigh(goods.getId(), scanItem.getWeigh(), apiLog.getId());
                            if (goods.getReagentInitWeight() != null) {
                                //处理”试剂初始净量异常“事件
                                if (goods.getReagentInitWeight().compareTo(new BigDecimal(scanItem.getWeigh())) > 0) {
                                    //事件描述
                                    String eventDesc = eventConfig.getEventDesc();
                                    eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(goods));
                                    eventDesc = eventDesc.replace("{{init-wt}}", goods.getReagentInitWeight().toString());
                                    eventDesc = eventDesc.replace("{{1st-wt}}", scanItem.getWeigh());
                                    eventDesc = eventDesc.replace("{{cab-door}}", doorName);
                                    eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
                                    //组合实体类
                                    BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
                                    event.setEventConfigId(eventConfig.getId());
                                    event.setEventTime(apiLog.getEventTime());
                                    event.setEventDesc(eventDesc);
                                    event.setGoodsId(goods.getId());
                                    event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
                                    event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
                                    event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
                                    event.setCabinetDoorId(cabinetDoorResp.getId());
                                    event.setEventUserId(apiLog.getUserId());
                                    event.setAdminId(apiLog.getAdminId());
                                    bizEventReplaceService.clearBizEvent(event);
                                    bizEventMapper.insert(event);

                                    //【邮件+短信】柜门管理员
                                    bizEventReplaceService.sendEmailForDoorAdmin(event);
                                    Map<String, String> content = new HashMap<>();
                                    content.put("reagentName", bizEventReplaceService.getGoodsNameForEntity(goods));
                                    content.put("reagentWeght", goods.getReagentInitWeight().toString());
                                    content.put("reagentInitWeght", scanItem.getWeigh());
                                    content.put("cabinetName", doorName);
                                    content.put("checkTime", DateUtil.formatDateTime(apiLog.getEventTime()));
                                    bizEventReplaceService.sendSmsForDoorAdmin(event, AliyunSmsUtil.ABNORMAL_NETWEIGHT_REAGENT, content);
                                }
                                //计算容器重量
                                bizGoodsService.handleReagentFistAvlData(goods, scanItem.getWeigh());
                            }
                        } else {
                            noWeighList.add(goods);
                        }
                        //触发事件-首次入柜未称重
                        this.addRecordForNoWeigh(noWeighList, apiLog, doorName, cabinetDoorResp);
                    }
                }
            }
        }
    }

    /**
     * 添加记录-首次入库未称重
     *
     * @param goodsList
     * @param apiLog
     */
    private void addRecordForNoWeigh(List<BizGoods> goodsList, DevApiLog apiLog, String doorName, BizCabinetDoorResp cabinetDoorResp) {
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("37");
        for (BizGoods item : goodsList) {
            String eventDesc = eventConfig.getEventDesc();
            //事件描述
            eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(item));
            eventDesc = eventDesc.replace("{{cab-door}}", doorName);
            eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
            eventDesc = eventDesc.replace("{{action}}", EventActionUtil.getApiAction(apiLog.getReqType()));
            //操作人
            String userName = bizEventReplaceService.getUserName(apiLog.getUserId());
            eventDesc = eventDesc.replace("{{user}}", userName);
            if (StringUtils.isNotBlank(apiLog.getAdminId())) {
                String adminName = bizEventReplaceService.getUserName(apiLog.getAdminId());
                eventDesc = eventDesc.replace("{{admin}}", adminName);
            } else {
                eventDesc = eventDesc.replace(",{{admin}}", "");
            }
            //组合实体类
            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
            event.setEventConfigId(eventConfig.getId());
            event.setEventTime(apiLog.getEventTime());
            event.setEventDesc(eventDesc);
            event.setGoodsId(item.getId());
            event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
            event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
            event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
            event.setCabinetDoorId(cabinetDoorResp.getId());
            event.setEventUserId(apiLog.getUserId());
            event.setAdminId(apiLog.getAdminId());
            bizEventReplaceService.clearBizEvent(event);
            bizEventMapper.insert(event);

            //【邮件】本次刷卡开门的卡主，柜门管理员
            bizEventReplaceService.sendEmailForUser(apiLog.getUserId(), event);
            bizEventReplaceService.sendEmailForDoorAdmin(event);
        }
    }


    @Override
    public void addRecordForScrapiGoodsInCab(List<BizGoods> goodsList, DevApiLog apiLog) {
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("25");
        //柜门信息
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        for (BizGoods goods : goodsList) {
            if (goods.getState() == 0) {
                String eventDesc = eventConfig.getEventDesc();
                //事件描述
                eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(goods));
                eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
                String userName = bizEventReplaceService.getUserName(apiLog.getUserId());
                eventDesc = eventDesc.replace("{{user}}", userName);
                //柜门名称
                String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
                eventDesc = eventDesc.replace("{{cab-door}}", doorName);
                //组合实体类
                BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
                event.setApiLogId(apiLog.getId());
                event.setEventConfigId(eventConfig.getId());
                event.setEventTime(apiLog.getEventTime());
                event.setEventDesc(eventDesc);
                event.setEventUserId(apiLog.getUserId());
                event.setGoodsId(goods.getId());
                event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
                event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
                event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
                event.setCabinetDoorId(cabinetDoorResp.getId());
                bizEventReplaceService.clearBizEvent(event);
                //类目切换
                bizEventReplaceService.itemSwitch(event, goods.getGoodsType());
                bizEventMapper.insert(event);

                //【邮件】本次刷卡开门的卡主，柜门管理员
                bizEventReplaceService.sendEmailForUser(apiLog.getUserId(), event);
                bizEventReplaceService.sendEmailForDoorAdmin(event);
            }
        }
    }

    @Override
    public void handleCabWeight(ScanDto dto, DevApiLog apiLog) {
        if (dto.getGoodsType() == 1) {
            //柜子信息
            BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
            //计算当前柜子在柜的试剂重量
            String totlaWeigh = bizGoodsMapper.totalWeighInCab(cabinetDoorResp.getCabinetInfoId());
            //转换为kg
            BigDecimal curWeigh = NumberUtil.div(totlaWeigh, "1000", 4);
            //更新柜子当前重量
            bizCabinetInfoMapper.updateCabCurSto(cabinetDoorResp.getCabinetInfoId(), curWeigh.toString());
            //查询对应配置
            BizEventConfig eventConfig = bizEventConfigMapper.selectById("39");
            if (StringUtils.isNotBlank(cabinetDoorResp.getReagentStorageMax())) {
                List<String> idList = dto.getItemList().stream().map(ScanItemDto::getGoodsId).collect(Collectors.toList());
                //获取物品列表
                List<BizGoods> goodsList = bizGoodsMapper.getListByIds(idList);
                //柜子名称
                String cabName = bizEventReplaceService.getCabName(apiLog.getCabinetCode());
                //操作人
                String userName = bizEventReplaceService.getUserName(apiLog.getUserId());
                for (BizGoods goods : goodsList) {
                    if (goods.getGoodsType() == 1) {
                        String eventDesc = eventConfig.getEventDesc();
                        //事件描述
                        eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(goods));
                        eventDesc = eventDesc.replace("{{cab}}", cabName);
                        eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
                        eventDesc = eventDesc.replace("{{action}}", EventActionUtil.getApiAction(apiLog.getReqType()));
                        eventDesc = eventDesc.replace("{{user}}", userName);
                        if (StringUtils.isNotBlank(apiLog.getAdminId())) {
                            String adminName = bizEventReplaceService.getUserName(apiLog.getAdminId());
                            eventDesc = eventDesc.replace("{{admin}}", adminName);
                        } else {
                            eventDesc = eventDesc.replace(",{{admin}}", "");
                        }
                        //组合实体类
                        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
                        event.setEventConfigId(eventConfig.getId());
                        event.setEventTime(apiLog.getEventTime());
                        event.setEventDesc(eventDesc);
                        event.setGoodsId(goods.getId());
                        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
                        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
                        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
                        event.setCabinetDoorId(cabinetDoorResp.getId());
                        event.setEventUserId(apiLog.getUserId());
                        event.setAdminId(apiLog.getAdminId());
                        bizEventReplaceService.clearBizEvent(event);
                        bizEventMapper.insert(event);

                        //【邮件+短信】本次刷卡开门的卡主，柜门管理员
                        bizEventReplaceService.sendEmailForUser(apiLog.getUserId(), event);
                        bizEventReplaceService.sendEmailForDoorAdmin(event);

                        Map<String, String> content = new HashMap<>();
                        content.put("reagentName", bizEventReplaceService.getGoodsNameForEntity(goods));
                        content.put("cabinet", cabName);
                        content.put("checkTime", DateUtil.formatDateTime(apiLog.getEventTime()));
                        content.put("operator", userName);
                        bizEventReplaceService.sendSmsForUser(apiLog.getUserId(), event, AliyunSmsUtil.REAGENT_CABINET_EMC, content);
                        bizEventReplaceService.sendSmsForDoorAdmin(event, AliyunSmsUtil.REAGENT_CABINET_EMC, content);
                    }
                }
            }
        }
    }

    @Override
    public void handleReagentClassClash(ScanDto dto, DevApiLog apiLog) {
        List<String> idList = dto.getItemList().stream().map(ScanItemDto::getGoodsId).collect(Collectors.toList());
        //获取物品列表
        List<BizGoods> goodsList = bizGoodsMapper.getListByIds(idList);
        //获取试剂列表-首次入库的
        List<BizGoods> reagentList = goodsList.stream().filter(item -> item.getGoodsType() == 1).collect(Collectors.toList());
        //list-试剂存储类型冲突
        List<BizGoods> clashList = new ArrayList<>();
        //柜门名称
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        for (BizGoods goods : reagentList) {
            if (goods.getUnknownFlag() == 0) {
                if (StringUtils.isNotBlank(cabinetDoorResp.getReagentStorageType())) {
                    //存放试剂类型冲突
                    if (!cabinetDoorResp.getReagentStorageType().equals(goods.getReagentClassId())) {
                        clashList.add(goods);
                    }
                }
            }
        }
        this.addRecordForClassClash(clashList, apiLog, doorName, cabinetDoorResp);
    }

    @Override
    public void addRecordForReagentUsageAbn(ScanDto dto, DevApiLog apiLog) {
        if (StringUtils.isNotBlank(apiLog.getReagentWeight())) {
            //获取物品信息
            BizGoods goods = bizGoodsMapper.selectById(dto.getGoodsId());
            if (goods.getGoodsType() == 1 && StringUtils.isNotBlank(goods.getReagentWeighNeedFlag())) {
                if (goods.getReagentWeighNeedFlag().equals("1") && goods.getReagentMaxErrorValue() != null) {
                    //最后次称重 (优先取最后的称重 否则取初始净重)
                    BigDecimal lastWeight = goods.getReagentLastWeigh() != null ? goods.getReagentLastWeigh() : goods.getReagentInitWeight();
                    if (lastWeight != null) {
                        //两次差额取绝对值
                        if (NumberUtil.sub(lastWeight.toString(), apiLog.getReagentWeight()).abs().compareTo(goods.getReagentMaxErrorValue()) > 0) {
                            //查询对应配置
                            BizEventConfig eventConfig = bizEventConfigMapper.selectById("35");
                            //事件描述
                            String eventDesc = eventConfig.getEventDesc();
                            eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(goods));
                            eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(DateUtil.date()));
                            eventDesc = eventDesc.replace("{{inv-wt}}", lastWeight.toString());
                            eventDesc = eventDesc.replace("{{1st-wt}}", apiLog.getReagentWeight());
                            //组合实体类
                            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
                            event.setApiLogId(apiLog.getId());
                            event.setEventConfigId(eventConfig.getId());
                            event.setEventTime(apiLog.getEventTime());
                            event.setEventDesc(eventDesc);
                            event.setGoodsId(goods.getId());
                            event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
                            event.setEventUserId(apiLog.getUserId());
                            bizEventReplaceService.clearBizEvent(event);
                            bizEventMapper.insert(event);

                            //【补全信息】因为仅称重还没柜子信息这里取物品的所属柜子信息
                            event.setCabinetGroupId(goods.getBioCabinetGroupId());
                            event.setCabinetId(goods.getBioCabinetId());
                            event.setCabinetDoorId(goods.getBioCabinetDoorId());
                            //【邮件】借用人+柜门管理员
                            bizEventReplaceService.sendEmailForDoorAdmin(event);
                            bizEventReplaceService.sendEmailForUser(apiLog.getUserId(), event);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void addRecordForRtnWeighCount(ScanDto dto, DevApiLog apiLog) {
        //获取物品信息
        BizGoods goods = bizGoodsMapper.selectById(dto.getGoodsId());
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("38");
        //柜子信息
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        //操作人
        String userName = bizEventReplaceService.getUserName(apiLog.getUserId());
        String eventDesc = eventConfig.getEventDesc();
        //事件描述
        eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(goods));
        eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
        eventDesc = eventDesc.replace("{{action}}", EventActionUtil.getApiAction(apiLog.getReqType()));
        eventDesc = eventDesc.replace("{{user}}", userName);
        if(StringUtils.isNotBlank(apiLog.getAdminId())){
            String adminName = bizEventReplaceService.getUserName(apiLog.getAdminId());
            eventDesc = eventDesc.replace("{{admin}}", adminName);
        }else{
            eventDesc = eventDesc.replace(",{{admin}}", "");
        }
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setGoodsId(goods.getId());
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
        event.setCabinetDoorId(cabinetDoorResp.getId());
        event.setEventUserId(apiLog.getUserId());
        event.setAdminId(apiLog.getAdminId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);

        //【邮件】本次刷卡开门的卡主，柜门管理员
        bizEventReplaceService.sendEmailForUser(apiLog.getUserId(), event);
        bizEventReplaceService.sendEmailForDoorAdmin(event);
    }

    @Override
    public void addRecordForGoodsBrOfUserAbn(GoodsLedgerPageResult goodsLedgerPageResult,ScanDto dto, DevApiLog apiLog) {
        //获取物品信息
        BizGoods goods = bizGoodsMapper.selectById(dto.getGoodsId());
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("18");
        //柜门信息
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        String eventDesc = eventConfig.getEventDesc();
        //事件描述
        eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(goods));
        eventDesc = eventDesc.replace("{{borrower}}", bizEventReplaceService.getUserName(goodsLedgerPageResult.getDeliveryUserId()));
        eventDesc = eventDesc.replace("{{returnees}}", bizEventReplaceService.getUserName(apiLog.getUserId()));
        eventDesc = eventDesc.replace("{{action}}", EventActionUtil.getApiAction(apiLog.getReqType()));
        eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
        eventDesc = eventDesc.replace("{{user}}", bizEventReplaceService.getUserName(apiLog.getUserId()));
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventUserId(apiLog.getUserId());
        event.setGoodsId(dto.getGoodsId());
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
        event.setCabinetDoorId(cabinetDoorResp.getId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);

        //【邮件】本次刷卡开门的卡主，柜门管理员，借用人
        bizEventReplaceService.sendEmailForUser(apiLog.getUserId(), event);
        bizEventReplaceService.sendEmailForDoorAdmin(event);
        bizEventReplaceService.sendEmailForUser(goodsLedgerPageResult.getDeliveryUserId(), event);
    }

    @Override
    public void addRecordForGoodsBrOfOverdue(GoodsLedgerPageResult goodsLedgerPageResult, ScanDto dto, DevApiLog apiLog) {
        //获取物品信息
        BizGoods goods = bizGoodsMapper.selectById(dto.getGoodsId());
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("20");
        //柜门信息
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        String eventDesc = eventConfig.getEventDesc();
        //事件描述
        eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(goods));
        eventDesc = eventDesc.replace("{{ret-time}}", DateUtil.formatDateTime(goodsLedgerPageResult.getReturnTime()));
        eventDesc = eventDesc.replace("{{act-ret-time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
        //超期时间
        String ovdStr = DateUtil.formatBetween(goodsLedgerPageResult.getReturnTime(), apiLog.getEventTime(), BetweenFormatter.Level.SECOND);
        eventDesc = eventDesc.replace("{{ovd-hour}}", ovdStr);
        eventDesc = eventDesc.replace("{{action}}", EventActionUtil.getApiAction(apiLog.getReqType()));
        eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
        eventDesc = eventDesc.replace("{{user}}", bizEventReplaceService.getUserName(apiLog.getUserId()));
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventUserId(apiLog.getUserId());
        event.setGoodsId(dto.getGoodsId());
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
        event.setCabinetDoorId(cabinetDoorResp.getId());
        event.setEventUserId(goodsLedgerPageResult.getDeliveryUserId());
        bizEventReplaceService.clearBizEvent(event);
        //类目切换
        bizEventReplaceService.itemSwitch(event, goodsLedgerPageResult.getGoodsType());
        bizEventMapper.insert(event);

        //【邮件】借用人，柜门管理员
        bizEventReplaceService.sendEmailForUser(goodsLedgerPageResult.getDeliveryUserId(), event);
        bizEventReplaceService.sendEmailForDoorAdmin(event);
    }

    /**
     * 添加记录-试剂存储属性冲突
     *
     * @param goodsList
     * @param apiLog
     */
    private void addRecordForClassClash(List<BizGoods> goodsList, DevApiLog apiLog, String doorName, BizCabinetDoorResp cabinetDoorResp) {
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("40");
        for (BizGoods item : goodsList) {
            String eventDesc = eventConfig.getEventDesc();
            //事件描述
            eventDesc = eventDesc.replace("{{goods}}", bizEventReplaceService.getGoodsNameForEntity(item));
            eventDesc = eventDesc.replace("{{cab-door}}", doorName);
            eventDesc = eventDesc.replace("{{time}}", DateUtil.formatDateTime(apiLog.getEventTime()));
            eventDesc = eventDesc.replace("{{action}}", EventActionUtil.getApiAction(apiLog.getReqType()));
            //操作人
            String userName = bizEventReplaceService.getUserName(apiLog.getUserId());

            eventDesc = eventDesc.replace("{{user}}", userName);
            if (StringUtils.isNotBlank(apiLog.getAdminId())) {
                String adminName = bizEventReplaceService.getUserName(apiLog.getAdminId());
                eventDesc = eventDesc.replace("{{admin}}", adminName);
            } else {
                eventDesc = eventDesc.replace(",{{admin}}", "");
            }
            //组合实体类
            BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
            event.setEventConfigId(eventConfig.getId());
            event.setEventTime(apiLog.getEventTime());
            event.setEventDesc(eventDesc);
            event.setGoodsId(item.getId());
            event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
            event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
            event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
            event.setCabinetDoorId(cabinetDoorResp.getId());
            event.setEventUserId(apiLog.getUserId());
            event.setAdminId(apiLog.getAdminId());
            bizEventReplaceService.clearBizEvent(event);
            bizEventMapper.insert(event);

            //【邮件+短信】本次刷卡开门的卡主，柜门管理员
            bizEventReplaceService.sendEmailForUser(apiLog.getUserId(), event);
            bizEventReplaceService.sendEmailForDoorAdmin(event);

            Map<String, String> content = new HashMap<>();
            content.put("reagentName", bizEventReplaceService.getGoodsNameForEntity(item));
            content.put("cabinet", doorName);
            content.put("checkTIme", DateUtil.formatDateTime(apiLog.getEventTime()));
            content.put("operator", userName);
            bizEventReplaceService.sendSmsForUser(apiLog.getUserId(), event, AliyunSmsUtil.REAGENT_STORAGE_CONFLICTS, content);
            bizEventReplaceService.sendSmsForDoorAdmin(event, AliyunSmsUtil.REAGENT_STORAGE_CONFLICTS, content);
        }
    }

}

